﻿using System.Windows;
using System.Windows.Controls;
using StudentManagement.Common;
using StudentManagement.Entity;
using StudentManagement.BLL;


namespace StudentManagement
{
    /// <summary>
    /// AlterStudentInfo.xaml 的交互逻辑
    /// </summary>
    public partial class AddStudentInfo : Window
    {
        public AddStudentInfo()
        {
            InitializeComponent();

        }
        EcanConvertToCh ecanConvertToCh = new EcanConvertToCh();
        AlterStudenInfoBussiness alterStudenInfoBussiness = new AlterStudenInfoBussiness();
        private void StuName_Change(object sender, TextChangedEventArgs e)
        {
            string stuName = StuName.Text.Trim();
            string stuAccount = ecanConvertToCh.convertCh(stuName).ToLower();
            int i = 1;
            if (alterStudenInfoBussiness.IsExistAccount(stuAccount))
            {
                StuAccount.Text = stuAccount.ToLower();
            }
            else
            {
                while (!alterStudenInfoBussiness.IsExistAccount(stuAccount))
                {
                    stuAccount = stuAccount + i;
                    i++;
                }
                StuAccount.Text = stuAccount;
            }

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            
            string stuName = StuName.Text.Trim();
            string stuAccount = StuAccount.Text.Trim();
            
            string stuSex = Male.IsChecked.Value ? "男":"女";
            string stuRemark = StuRemark.Text.Trim();
            StudentInfoEntity studentInfoEntity = new StudentInfoEntity();
            studentInfoEntity.Name = stuName;
            studentInfoEntity.Account = stuAccount;
            if (!alterStudenInfoBussiness.IsExistAccount(stuAccount))
            {
                MessageBox.Show("当前帐号已存在，请检查！");
                return;
            }
            studentInfoEntity.Sex = stuSex;
            studentInfoEntity.Remark = stuRemark;
            if (stuRemark.Length >= 255)
            {
                MessageBox.Show("备注长度超出范围，最大长度为255，请检查！");
                return;

            }
            studentInfoEntity.Password = "123456";
            
            bool res = alterStudenInfoBussiness.AddStudentInfo(studentInfoEntity);
            if (res)
            {
                MessageBox.Show("添加成功！");
            }
            else
            {
                MessageBox.Show("添加失败！");
            }
                
        }
    }
}
